using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [MenuFilter]
    public class MemberController : AdminController
    {
        public ActionResult Index()
        {
            List<Place> deep1List = new PlaceService().GetDeep1();
            ViewData["deep1List"] = deep1List;
            List<Place> deep2List = new PlaceService().GetDeep2();
            ViewData["deep2List"] = deep2List;
            List<Place> deep3List = new PlaceService().GetDeep3();
            ViewData["deep3List"] = deep3List;
            return View();
        }

        /// <summary>
        /// 获取首页列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetListPage()
        {
            LayTableModel result = new LayTableModel();
            MemberService service = new MemberService();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string field = Request.Params["field"];//排序字段名
                string order = Request.Params["order"];//排序方式：desc, asc, null(空对象，默认排序)
                //参数
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string[] par = { "UserName", "RealName", "Phone", "Email", "Status", "ProvinceCode", "CityCode", "DistrictCode", "SchoolName", "JoinType" };

                foreach (string item in par)
                {
                    if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                    {
                        dic.Add(item, Request.Params[item]);
                    }
                }
                int count = 0;

                var list = service.GetListPage(page, limit, dic, field, order, out count);

                result.data = list;
                result.count = count;

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 添加修改视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0)
        {
            MemberOutput model = null;
            MemberService service = new MemberService();
            if (Id > 0)
            {
                model = service.Get<MemberOutput>(Id);
            }
            else
            {
                model = new MemberOutput()
                {
                    JoinTime = DateTime.Now
                };

            }
            List<Place> deep1List = new PlaceService().GetDeep1();
            ViewData["deep1List"] = deep1List;
            List<Place> deep2List = new PlaceService().GetDeep2();
            ViewData["deep2List"] = deep2List;
            List<Place> deep3List = new PlaceService().GetDeep3();
            ViewData["deep3List"] = deep3List;
            return View(model);
        }

        /// <summary>
        /// 查看视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Look(int Id = 0)
        {
            MemberOutput model = null;
            MemberService service = new MemberService();
            if (Id > 0)
            {
                model = service.Get<MemberOutput>(Id);
            }
            else
            {
                model = new MemberOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Add(MemberInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                if (!new MemberService().Exist(model.UserName, null))
                {
                    int result = new MemberService().Add(model);
                    if (result > 0)
                    {
                        response.Success = true;
                        response.Message = "添加成功";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "添加失败";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "账号已经存在";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(MemberInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                if (!new MemberService().Exist(model.UserName, model.Id))
                {
                    int id = new MemberService().Edit(model);
                    response.Success = true;
                    response.Message = "修改成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "账号已经存在";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new MemberService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Edit")]
        public JsonResult ResetPassWord(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new MemberService().ResetPassWord(IdList);
                response.Success = true;
                response.Message = "重置成功,默认密码：123456";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        [MenuRoute("Edit")]
        public JsonResult UpdateStatus(int Id)
        {
            ResponseModel response = new ResponseModel();
            MemberService serviece = new MemberService();

            try
            {
                string status = Request.Params["status"];
                if (!string.IsNullOrWhiteSpace(status))
                {
                    serviece.UpdateStatus(Id, int.Parse(status));

                    response.Success = true;
                    response.Message = "修改成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "参数错误";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
    }
}
